package cz.muni.fi.pv168.confusinglibrary.libs;

import java.util.Date;

/**
 * This class represents authors.
 *
 * @author Riema
 */
public class Author {

    /**
     * Identification number - unique for every instance (or null after creating)
     */
    private Long id = null;
    /**
     * Represents first name of an author,
     * can also contain middle name/s
     * or not exists at all ("" or null)
     */
    private String name;
    /**
     * Represents surname of an author (or  pseudonym),
     * cannot be null
     */
    private String surname = "";
    /**
     * Date when was author born
     * null if date is unknown
     */
    private Date born;
    /**
     * Date when author died
     * null if date is unknown or author is still alive
     */
    private Date died;

    /**
     * Constructor without parameters,
     * for changing attributes use setters
     */
    public Author() {
    }

    public Date getBorn() {
        return this.born;
    }

    /**
     *
     * @param born date when author was born, can be null
     * @throws IllegalArgumentException if (date >= current time) or (date > died)
     */
    public void setBorn(Date born) {
        if (born == null) {
            this.born = null;
        } else {
            if (this.isDead() && this.died.before(born)) {
                throw new IllegalArgumentException("Wrong date - too early");
            }
            Date currentTime = new Date();
            if (born.after(currentTime)) {
                throw new IllegalArgumentException("Wrong date - too late");
            }

            this.born = born;
        }
    }

    public Date getDied() {
        return this.died;
    }

    /**
     *
     * @param died date when author died, can bo null
     * @throws IllegalArgumentException if (died < born) or (died >= current time) (we're not murderers)
     */
    public void setDied(Date died) {
        if (died != null) {
            if (this.born != null && died.before(this.born)) {
                throw new IllegalArgumentException("Wrong date - too early");
            }

            Date currentTime = new Date();
            if (died.after(currentTime)) {
                throw new IllegalArgumentException("Wrong date - too late");
            }
        }

        this.died = died;
    }

    public String getName() {
        return this.name;
    }

    /**
     *
     * @param name first name of author, can be null
     */
    public void setName(String name) {
        this.name = name;
    }

    public Long getId() {
        return this.id;
    }

    /**
     *
     * @param id default null, change should  be done only by AuthorManager
     */
    public void setId(Long id) {
        this.id = id;
    }

    public String getSurname() {
        return this.surname;
    }

    /**
     *
     * @param surname surname of author
     * @throws NullPointerException if patametr is null
     * @throws IllegalArgumentException if patamert is ""
     */
    public void setSurname(String surname) {
        if (surname == null) {
            throw new NullPointerException("surname");
        }
        if (surname.equals("")) {
            throw new IllegalArgumentException("surname");
        }
        this.surname = surname;
    }

    // equals are if same id
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Author)) {
            return false;
        }

        Author other = (Author) obj;

        if (other.id == null || this.id == null) {
            return false;
        }

        return this.id.equals(other.id);
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 89 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    // TODO something logical for writing out author
    @Override
    public String toString() {
        return name + " " + surname + " born = " + born + ", died = " + died;
    }

    /**
     *
     * @return true if author is dead else false
     */
    public boolean isDead() {
        return this.died != null;
    }
}
